<style>
.pure_form_extra { background: #fff; padding: 10px; border: 1px solid #aaa; border-radius: 8px; font-size: 12px; font-family: Trebuchet MS; }
.pure_form_extra label { font-size: 12px !important; }
.pure_form_od { background: #fff; padding: 10px; border: 1px solid #aaa; border-radius: 8px; }
.pure_form_od label { font-size: 14px !important; }
.pure_form_od label.subtitle { font-weight: bold !important; }
.pure_form_od a { font-size: 12px !important; color: #000; }
.pure_form_pay { background: #fff; padding: 10px; border: 1px solid #aaa; border-radius: 8px; }
.pure_form_pay legend { border-bottom: 1px solid #ddd; padding-bottom: 5px; padding-left: 20px; font-weight: bold;  font-family: Trebuchet MS; }
.pure_form_pay div { font-size: 12px; font-family: Trebuchet MS; }
.pure_button_cart { margin-left: 5px; font-weight: bold; font-size: 14px !important; }
</style>

<?php
$user = null;
if (isset($_SESSION['user'])) {
  $user = json_decode($_SESSION['user']);
}

$order_number = Utils::get_param('od', NULL);

$is_allowed = 0;

$opts = new stdClass();
$opts->cd = 't.order_number = :v1';
$opts->cdv = array(':v1' => $order_number);
$data = $orm->get('pb_ecom_order', NULL, NULL, NULL, NULL, $opts)->d;

if (count($data) > 0) {
  if ($user && $data[0]->customer_id == $user->id) {
    $is_allowed = 1;
  }
  if ($data[0]->customer_id == 0) {
    $is_allowed = 1;
  }
  
  $opts = new stdClass();
  $opts->cd = 't.id = :v1';
  $opts->cdv = array(':v1' => $data[0]->payment_id);
  $data[0]->__payment = $orm->get('pb_ecom_mthd_payment', NULL, NULL, NULL, NULL, $opts)->d[0];
}

$currs = $orm->get('pb_ecom_currency', 't.title', 'ASC', NULL, NULL, NULL)->d;
?>

<table cellpadding=0 cellspacing=0>
  <tr>
    <td style="width: 730px; vertical-align: top; padding-right: 10px;">
      <?php if (count($data) == 0) { ?>
      <div class="pure-form pure-form-aligned pure_form_extra">找不到订单 <?php echo $order_number; ?></div>
      <?php } else if ($is_allowed == 0) { ?>
      <div class="pure-form pure-form-aligned pure_form_extra">您无权限查看该订单</div>
      <?php } else if ($data[0]->is_paid == 1) { ?>
      <div class="pure-form pure-form-aligned pure_form_extra">该订单已支付</div>
      <?php } else if ($data[0]->is_cancel == 1) { ?>
      <div class="pure-form pure-form-aligned pure_form_extra">该订单已被取消</div>
      <?php } else { ?>
      <div class="pure-form pure-form-aligned pure_form_od">
        <div class="pure-control-group">
          <label class="subtitle">订单编号</label>
          <label><?php echo $data[0]->order_number; ?></label>
          <a href="#" onclick="window.open('<?php echo URL . HOME_DIR . $app_data->sys->php_name . '.php?i=' . $app_data->param_id . '&__file=index_order&od=' . $order_number; ?>')">订单详情</a>
        </div>
        <div class="pure-control-group">
          <label class="subtitle">货品总额</label>
          <label id="total"></label>
        </div>
        <div class="pure-control-group">
          <label class="subtitle">运费总额</label>
          <label id="delivery_subtotal"></label>
        </div>
        <div class="pure-control-group">
          <label class="subtitle">全部总额</label>
          <label id="all_total"></label>
        </div>
        <div class="pure-control-group">
          <label class="subtitle">快递公司</label>
          <label id="all_total"><?php echo $data[0]->delivery_name; ?></label>
        </div>
        <div class="pure-control-group">
          <label class="subtitle">支付方式</label>
          <label id="all_total"><?php echo $data[0]->payment_name; ?></label>
        </div>
      </div>
      
      <div>&nbsp;</div>
      
      <div class="pure_form_pay">
        <legend><?php echo $data[0]->payment_name; ?></legend>
        <div style="margin: 20px;">
          <?php echo $data[0]->__payment->description; ?>
          <div>&nbsp;</div>
          <?php if ($data[0]->__payment->code == P_PAYPAL) { ?>
          <form id="paypal_form" method="post" action="<?php echo PAYPAL_URL; ?>" target="_parent">
            <input type="hidden" name="cmd" value="_xclick"></input>
            
            <input type="hidden" name="business" value="<?php echo $data[0]->__payment->account; ?>"></input>
            <input type="hidden" name="item_name" value="<?php echo $data[0]->order_number; ?>"></input> 
            <input type="hidden" name="item_number" value=""></input>
            <input type="hidden" name="amount" value="<?php echo $data[0]->total + $data[0]->delivery_subtotal; ?>"></input>
            <input type="hidden" name="currency_code" value="<?php echo $data[0]->curr; ?>"></input> 
            
            <input type="hidden" name="no_shipping" value="1"></input> 
            <input type="hidden" name="no_note" value="1"></input> 
            
            <input type="hidden" name="charset" value="utf-8"></input> 
            <input type="hidden" name="first_name" value="<?php echo $data[0]->first_name; ?>">
            <input type="hidden" name="last_name" value="<?php echo $data[0]->last_name; ?>">
            <input type="hidden" name="address1" value="<?php echo ($data[0]->address1); ?>">
            <input type="hidden" name="address2" value="<?php echo ($data[0]->address2); ?>">
            <input type="hidden" name="city" value="<?php echo $data[0]->city; ?>">
            <input type="hidden" name="country" value="<?php echo $data[0]->country; ?>">
            <input type="hidden" name="state" value="Not Applicable">
            <input type="hidden" name="zip" value="<?php echo $data[0]->zip; ?>">
            
            <input type="hidden" name="night_phone_a" value="">
            <input type="hidden" name="night_phone_b" value="<?php echo $data[0]->phone; ?>">
            <input type="hidden" name="night_phone_c" value="">
            
            <input type="hidden" name="email" value="<?php echo $data[0]->email; ?>">
  
            <input type="hidden" name="notify_url" value="<?php echo URL . HOME_DIR . '__ipn.php'; ?>"></input>
            <input type="hidden" name="return" value="<?php echo URL . HOME_DIR . $app_data->sys->php_name . '.php?i=' . ECOM_ID . '&__file=index_paynow&od=' . $order_number; ?>" /></input>
            <input type="hidden" name="cancel_return" value="<?php echo URL . HOME_DIR . $app_data->sys->php_name . '.php?i=' . ECOM_ID . '&__file=index_paynow&od=' . $order_number; ?>" /></input>
  
            <button type='submit'  class="pure-button pure-button-active pure_button_cart"><i class="fa fa-money fa-lg"></i> Pay it now</button>
          </form>
          <?php } ?>
        </div>
      </div>
      <?php } ?>
    </td>
    <td style="width: 230px; vertical-align: top;"><?php include PLGN_DIR . basename(dirname(__FILE__)) . '/inc.rightbar.php'; ?></td>
  </tr>
</table>
<div>&nbsp;</div>

<script type="text/javascript">
  var __currs = JSON.parse('<?php echo addslashes(json_encode($currs)); ?>');
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __is_allowed = '<?php echo $is_allowed; ?>';
  
  $(function(){
    __curr = Cookie.get_cookie('curr');
    for (var idx in __currs) {
      var itm = __currs[idx];
      if (itm.title == __data[0].curr) {
        __curr = itm;
      }
    }
    
    render();
  });


  function render() {
    render_data();
  };

  function render_data() {
    if (__data.length > 0 && __is_allowed == 1 && __data[0].is_paid == 0) {
      $('#total').append(accounting.formatMoney(__data[0].total, __curr.dollar_symbol));
      $('#delivery_subtotal').append(accounting.formatMoney(__data[0].delivery_subtotal, __curr.dollar_symbol));
      $('#all_total').append(accounting.formatMoney(parseFloat(__data[0].total, 10) + parseFloat(__data[0].delivery_subtotal, 10), __curr.dollar_symbol));
    }
  };
</script>